<?php

namespace app\admin\model\choi;

use think\Model;
use traits\model\SoftDelete;

class CourseOrder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'course_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'classdata_text',
        'payment_text',
        'status_text'
    ];
    

    
    public function getClassdataList()
    {
        return ['1' => __('Classdata 1'), '2' => __('Classdata 2')];
    }

    public function getPaymentList()
    {
        return ['1' => __('Payment 1'), '2' => __('Payment 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6')];
    }

    public function getStatus()
    {
        return ['2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getClassdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['classdata']) ? $data['classdata'] : '');
        $list = $this->getClassdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaymentTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['payment']) ? $data['payment'] : '');
        $list = $this->getPaymentList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 更新订单状态
     */
    public static function updateStatus($courseOrderId, $status)
    {
        return self::where('course_order_id', $courseOrderId)->update(['status' => $status]);
    }

    public function teacher()
    {
        return $this->hasOne(Teacher::class, 'teacher_id', 'teacher_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id');
    }

    public static function getWaitCount()
    {
        return self::where('status', 2)->count();
    }

    public static function getFinishCount()
    {
        return self::where('status', 3)->count();
    }

}
